using LiteNetLib.Utils;

namespace LiteNetLib
{
	public sealed class NetPacketReader : NetDataReader
	{
		private NetPacket _packet;

		private readonly NetManager _manager;

		private readonly NetEvent _evt;

		internal NetPacketReader(NetManager manager, NetEvent evt)
		{
			_manager = manager;
			_evt = evt;
		}

		internal void SetSource(NetPacket packet)
		{
			if (packet != null)
			{
				_packet = packet;
				SetSource(packet.RawData, packet.GetHeaderSize(), packet.Size);
			}
		}

		public void Recycle()
		{
			Clear();
			if (_packet != null)
			{
				_manager.NetPacketPool.Recycle(_packet);
			}
			_packet = null;
			_manager.RecycleEvent(_evt);
		}
	}
}
